/*
 * @Author: wei_jt
 * @Date: 2021-01-07 11:40:11
 * @LastEditors: zhong_m
 * @LastEditTime: 2022-01-04 15:20:51
 * @Description: 可自动清除message
 */
import { ElMessage, ElMessageBox } from 'element-plus';

let messageInstance = null;
export const resetMessage = (options) => {
	if (messageInstance) {
		messageInstance.close();
	}
	messageInstance = ElMessage(options);
};

['error', 'success', 'info', 'warning'].forEach((type) => {
	resetMessage[type] = (options) => {
		if (typeof options == 'string') {
			options = {
				message: options
			};
		}
		options.type = type;
		return resetMessage(options);
	};
});

const confirmMap = new Map();
export const confirmHandler = (title, msg, cb) => {
	console.log(title, msg, cb);
	if (confirmMap.has(title)) {
		return false;
	} else {
		confirmMap.set(title, true);
	}
	ElMessageBox.confirm(msg, title, {
		confirmButtonText: '确定',
		type: 'warning',
		showClose: false,
		closeOnClickModal: true,
		confirmButtonClass: 'fks-icon-check',
		showCancelButton: false,
		callback: (...args) => {
			confirmMap.delete(title);
			cb(...args);
		}
	});
};
